/**
 * 
 */
package com.swim.recruitment.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.swim.recruitment.entity.Resume;
import com.swim.recruitment.dao.ResumeDao;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 求职简历Service
 * @author ljw
 * @version 2023-04-13
 */
@Service
@Transactional(readOnly=true)
public class ResumeService extends CrudService<ResumeDao, Resume> {
	
	@Value("${server.context-path}")
	private String contextPath;
	
	/**
	 * 获取单条数据
	 * @param resume
	 * @return
	 */
	@Override
	public Resume get(Resume resume) {
		Resume result = super.get(resume);
		if (result != null) {
			//获取简历图片图片
			List<FileUpload> fileUploadList = FileUploadUtils.findFileUpload(result.getId(), "resume_image");
			if (CollectionUtils.isNotEmpty(fileUploadList)) {
				List<String> resumeImageUrlList = new ArrayList<String>();	//简历图片图片url集合
				for (FileUpload fileUploadListOne : fileUploadList) {
					resumeImageUrlList.add(contextPath + "/userfiles/fileupload/" + fileUploadListOne.getFileEntity().getFilePath() + fileUploadListOne.getFileEntity().getFileId() + "." + fileUploadListOne.getFileEntity().getFileExtension());
				}
				result.setResumeImageUrlList(resumeImageUrlList);
			}
			//获取头像图片url
			List<FileUpload> fileUploadListOne1 = FileUploadUtils.findFileUpload(result.getId(), "avatar_image");
			if (CollectionUtils.isNotEmpty(fileUploadListOne1)) {
				result.setAvatarImageUrl(contextPath + "/userfiles/fileupload/" + fileUploadListOne1.get(0).getFileEntity().getFilePath() + fileUploadListOne1.get(0).getFileEntity().getFileId() + "." + fileUploadListOne1.get(0).getFileEntity().getFileExtension());	//头像图片url
			}
		}
		return result;
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param resume
	 * @return
	 */
	@Override
	public Page<Resume> findPage(Page<Resume> page, Resume resume) {
		Page<Resume> pageList = super.findPage(page, resume);
		List<Resume> resumeList = pageList.getList();
		if (!CollectionUtils.isEmpty(resumeList)) {
			for (Resume resumeOne : resumeList) {
				//获取简历图片url集合
				List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(resumeOne.getId(), "resume_image");
				if (CollectionUtils.isNotEmpty(fileUploadListOne)) {
					List<String> resumeImageUrlList = new ArrayList<String>();	//简历图片url集合
					for (FileUpload fileUpload : fileUploadListOne) {
						resumeImageUrlList.add(contextPath + "/userfiles/fileupload/" + fileUpload.getFileEntity().getFilePath() + fileUpload.getFileEntity().getFileId() + "." + fileUpload.getFileEntity().getFileExtension());
					}
					resumeOne.setResumeImageUrlList(resumeImageUrlList);
				}
				//获取头像图片url
				List<FileUpload> fileUploadListOne1 = FileUploadUtils.findFileUpload(resumeOne.getId(), "avatar_image");
				if (CollectionUtils.isNotEmpty(fileUploadListOne1)) {
					resumeOne.setAvatarImageUrl(contextPath + "/userfiles/fileupload/" + fileUploadListOne1.get(0).getFileEntity().getFilePath() + fileUploadListOne1.get(0).getFileEntity().getFileId() + "." + fileUploadListOne1.get(0).getFileEntity().getFileExtension());	//头像图片url
				}
			}
		}
		pageList.setList(resumeList);
		return pageList;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param resume
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Resume resume) {
		super.save(resume);
		// 保存技能证书图片
		FileUploadUtils.saveFileUpload(resume.getId(), "resume_image");
		// 保存头像图片
		FileUploadUtils.saveFileUpload(resume.getId(), "avatar_image");
	}
	
	/**
	 * 更新状态
	 * @param resume
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Resume resume) {
		super.updateStatus(resume);
	}
	
	/**
	 * 删除数据
	 * @param resume
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Resume resume) {
		super.delete(resume);
	}
	
}